<?php
namespace app\models;
use common\component\tool\Tool;
use app\models\Basic;

use Yii;

class ContentFlag extends Basic
{
    
    public static function tableName()
    {
        return '{{%content_flag}}';
    }

    public function rules()
    {
        return [
            ['app_id', 'required', 'message' => 'app_id不能为空', 'on' => 'add'],
            ['app_id', 'integer', 'message' => 'app_id必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'app_id最小存储0', 
                        'max'=>18446744073709551615, 'tooBig' => 'app_id最大存储18446744073709551615'],
            ['flag', 'required', 'message' => 'flag不能为空', 'on' => 'add'],
            ['flag', 'string', 'message' => 'flag必须是字符串',
                        'min' => 1, 'tooShort' => 'flag最短1位',
                        'max' => 1, 'tooLong' => 'flag最长1位'],
            /*注释原因： 排序的时候，只需要修改排序的值*/
            // ['flagname', 'required', 'message' => 'flagname不能为空'],
            ['flagname', 'string', 'message' => 'flagname必须是字符串',
                        'min' => 2, 'tooShort' => 'flagname最短2位',
                        'max' => 16, 'tooLong' => 'flagname最长16位'],
            ['zhsorts', 'string', 'message' => 'zhsorts必须是字符串'],
            ['ensorts', 'string', 'message' => 'ensorts必须是字符串'],
            ['jpsorts', 'string', 'message' => 'jpsorts必须是字符串'],
            ['krsorts', 'string', 'message' => 'krsorts必须是字符串'],
            ['sys', 'integer', 'message' => 'sys必须为数字，或数字字符串'],
            ['sys', 'in', 'range' => array(1, 2), 'message' => 'sys值必须在1~2中选择'],
        ];
    }


    /*添加*/
    public function addFlag($data, $app_id)
    {
        $this->scenario = 'add';
        $data['ContentFlag']['app_id'] = $app_id;
        $data['ContentFlag']['lastmodify_time'] = date('Y-m-d H:i:s', time());
        // P($data);
        if($this->load($data) and $this->validate()){
            // P($this);
            if($this->save(false)){
                return true;
            }
        }
        return false;
    }

    /*
    修改
    $data       修改的数据
    $app_id 
    */
    public function modifyFlag($data, $id)
    {
        // P($data);
        $contentFlag = self::find()->where('id = :id', [':id' => $id])->one();
        // P($contentFlag);
        if(!is_null($contentFlag)){
            if($this->load($data) and $this->validate()){
                if(isset($this->flagname)){
                    $contentFlag->flagname = $this->flagname;
                }
                if(isset($this->zhsorts)){
                    $contentFlag->zhsorts = $this->zhsorts;
                }
                if(isset($this->ensorts)){
                    $contentFlag->ensorts = $this->ensorts;
                }
                if(isset($this->jpsorts)){
                    $contentFlag->jpsorts = $this->jpsorts;
                }
                if(isset($this->krsorts)){
                    $contentFlag->krsorts = $this->krsorts;
                }
                // P($contentFlag);
                if($contentFlag->save(false)){
                    return true;
                }
            }
        };
        return false;
    }

    
}